<template>
  <div class="order-detail-container" v-if="order">
    <div class="order-detail-card">
      <img :src="order.img || `/src/assets/tea/longjing.jpg`" alt="茶叶图片" />
      <div class="order-detail-info">
        <h2>订单详情</h2>
        <div class="order-detail-title">订单号：{{ order.id }}</div>
        <div class="order-detail-status">状态：{{ order.status }}</div>
        <div class="order-detail-price">总价：¥{{ order.totalPrice }}</div>
        <div class="order-detail-tea">茶叶ID：{{ order.teaId }}</div>
        <div class="order-detail-qty">数量：{{ order.quantity }}</div>
        <button v-if="order.status !== '已支付'" @click="payOrder">支付订单</button>
        <div v-if="success" class="success">{{ success }}</div>
        <div v-if="error" class="error">{{ error }}</div>
      </div>
    </div>
  </div>
  <div v-else>加载中...</div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from '../api/axios';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();
const order = ref<any>(null);
const success = ref('');
const error = ref('');

const fetchOrder = async () => {
  try {
    const res = await axios.get(`/order/${route.params.id}`);
    order.value = res.data;
  } catch {
    router.push('/orders');
  }
};

const payOrder = async () => {
  try {
    // 假设后端 /order/pay/{id} 完成支付
    await axios.post(`/order/pay/${order.value.id}`);
    success.value = '支付成功！';
    fetchOrder();
  } catch (e: any) {
    error.value = e.response?.data || '支付失败';
  }
};

onMounted(fetchOrder);
</script>

<style scoped>
.order-detail-container {
  max-width: 1200px;
  margin: 40px auto;
  padding: 32px;
  background: #fffbe6;
  border-radius: 16px;
  box-shadow: 0 4px 24px #ffe7ba;
  min-height: 480px;
}
.order-detail-card {
  display: flex;
  align-items: center;
  gap: 32px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px #ffd591;
  padding: 24px;
}
.order-detail-card img {
  width: 160px;
  height: 160px;
  object-fit: cover;
  border-radius: 12px;
  margin-right: 16px;
}
.order-detail-info {
  flex: 1;
}
.order-detail-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 8px;
}
.order-detail-status {
  font-size: 15px;
  color: #3b82f6;
  margin-bottom: 8px;
}
.order-detail-price {
  color: #ff6600;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
}
.order-detail-tea, .order-detail-qty {
  font-size: 15px;
  margin-bottom: 6px;
}
.order-detail-card button {
  background: #ff6600;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 10px 24px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 12px;
}
.success {
  color: green;
  margin-top: 8px;
}
.error {
  color: red;
  margin-top: 8px;
}
</style>
